import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.util.StringTokenizer;
import java.util.Vector;

import net.didion.jwnl.data.Synset;
import net.didion.jwnl.data.Word;
import chatsegmenter.LexChain;
import chatsegmenter.SystemParam;
import chatsegmenter.helper.Position;
import chatsegmenter.helper.UttTaggedSenseWord;
import chatsegmenter.helper.UttTaggedWord;
import chatsegmenter.helper.WordNetHelper;


public class Main {

	public static void main (String args[]) throws Exception
	{
		if (args.length != 2)
		{
			System.out.println("Error, use 2 arguments");
		}
		else
		{
			WordNetHelper.getInstance().initialize("file_properties.xml");
			Vector<UttTaggedWord> words = LexChain.wordsFromFile(args[0], false, false, null);
			Vector<UttTaggedSenseWord> wsdWords = LexChain.WSDFromWords(words, false, null);
			
			Vector<String> newWords = new Vector<String> ();
			for (int i = 0; i < wsdWords.size(); i++)
			{
				UttTaggedSenseWord sense = wsdWords.elementAt(i);
				Synset syn = sense.getSense();
				String lemma = sense.getIndexWord().getLemma();
				newWords.add(lemma);
				System.out.println(lemma + " " + sense.getSense().getPOS()+ " " + sense.getPosition().line + " " + sense.getPosition().index);
				Word[] WNWords = syn.getWords();
				for (int j = 0; j < WNWords.length; j++)
				{
					Word w = WNWords[j];
					if (w.getLemma().equals(lemma) == false)
					{
						newWords.set(i, w.getLemma());
					}
				}
				
			}
			
			int i = 0;
			
			String delim = " ,.:!?)([]{}\";/\\+=-_@#$%^&*|";
			
			BufferedReader in = new BufferedReader (new FileReader(args[0]));
			BufferedWriter out = new BufferedWriter(new FileWriter(args[1]));
			
			String line = new String();
			int lineCount = 0;
			while ((line = in.readLine())!=null)
			{
				int tkCount = 0;
				StringTokenizer st = new StringTokenizer(line, delim);
				String substr = new String(line);
				String output = new String("");
				while (st.hasMoreTokens())
				{
					String word = st.nextToken();
					int pos = substr.indexOf(word);
					output = output.concat(substr.substring(0, pos));
					
					if (i < wsdWords.size())
					{
						Position p = wsdWords.elementAt(i).getPosition();
						if (p != null && p.line == lineCount && p.index == tkCount)
						{
							output = output.concat(newWords.elementAt(i));
							//System.out.println(newWords.elementAt(i));
							i++;
						}
						else
						{
							output = output.concat(word);
						}
					}

					if (pos + word.length() < substr.length())
					{
						substr = substr.substring(pos + word.length());
					}
					tkCount++;
				}
				System.out.println(output);
				out.write(output + "\n");
				lineCount++;
			}
			out.close();
		}
	}
}
